#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdint.h>
#include "zend_long.h"
//==============================================================================
typedef struct {
	int32_t s1;
	int32_t s2;
	int seeded;
} php_lcg_globals;
//==============================================================================
#define MODMULT(a, b, c, m, s) q = s/a;s=b*(s-a*q)-c*q;if(s<0)s+=m
//==============================================================================
#define GENERATE_BAD_SEED(a, b, c, d, e) (((zend_long) (c * b)) ^ ((zend_long) (1000000.0 * bad_php_combined_lcg(a,b,c,d,e))))
//==============================================================================
static void bad_lcg_seed(php_lcg_globals* lcg, int pid, long epoch, long uepoch, int uepochOffset)
{
	lcg->s1 = epoch ^ (uepoch<<11);
	lcg->s2 = (zend_long) pid;
	lcg->s2 ^= ((uepoch + uepochOffset)<<11);
	lcg->seeded = 1;
}
//==============================================================================
double bad_php_combined_lcg(php_lcg_globals* lcg, int pid, long epoch, long uepoch, int uepochOffset)
{
	int32_t q;
	int32_t z;

	if (!lcg->seeded) {
		bad_lcg_seed(lcg, pid, epoch, uepoch, uepochOffset);
	}

	MODMULT(53668, 40014, 12211, 2147483563L, lcg->s1);
	MODMULT(52774, 40692, 3791, 2147483399L, lcg->s2);

	z = lcg->s1 - lcg->s2;
	if (z < 1) {
		z += 2147483562;
	}

	return z * 4.656613e-10;
}
//==============================================================================
int main(void){
    php_lcg_globals test;

	int pid = ${K};//--- PID
	long epoch = ${L};//--- EPOCH TIME
    long uepoch = 0;//--- MICROSECONDS
    int uepochOffset = 0;//--- OFFSET
    const int MAX_UEPOCH_OFFSET = 14;

    test.s1 = 0;
    test.s2 = 0;
    test.seeded = 0;

    for(uepoch = 1; uepoch < 1000000; uepoch++){
        for(uepochOffset = 0; uepochOffset <= MAX_UEPOCH_OFFSET; uepochOffset++){
            test.seeded = 0;
            printf("%ld\n", GENERATE_BAD_SEED(&test, pid, epoch, uepoch, uepochOffset));
        }
    }
    return 0;
}
//==============================================================================
